﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ChatDAL;

namespace ChatASP
{
    public partial class AdminPannel : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void GetUsers(object sender, EventArgs e)
        {
            List<User> users = ChatManagement.GetAllUsers();
            UsersList.DataSource = users;
            UsersList.DataBind();
            UsersList.Visible = true;
            ChannelsList.Visible = false;
            MessagesList.Visible = false;
            FilterLabel.Visible = false;
            ChannelBox.Visible = false;
            SenderBox.Visible = false;
            RecieverBox.Visible = false;
        }

        protected void GetChannels(object sender, EventArgs e)
        {
            List<ChatDAL.Channel> channels = ChatManagement.GetAllChannels();
            ChannelsList.DataSource = channels;
            ChannelsList.DataBind();
            UsersList.Visible = false;
            ChannelsList.Visible = true;
            MessagesList.Visible = false;
            FilterLabel.Visible = false;
            ChannelBox.Visible = false;
            SenderBox.Visible = false;
            RecieverBox.Visible = false;
        }

        protected void GetMessages(object sender, EventArgs e)
        {
            List<Message> messages = ChatManagement.GetAllMessages();
            MessagesList.DataSource = messages;
            MessagesList.DataBind();
            UsersList.Visible = false;
            ChannelsList.Visible = false;
            MessagesList.Visible = true;
            FilterLabel.Visible = true;
            ChannelBox.Visible = true;
            SenderBox.Visible = true;
            RecieverBox.Visible = true;
        }

        protected void FilterMessages(object sender, EventArgs e)
        {
            List<Message> messages = new List<Message>();
            MessagesList.DataSource = messages;
            MessagesList.DataBind();
            string messageSender = SenderBox.Text;
            string channel = ChannelBox.Text;
            if (channel != string.Empty)
            {
                int channelID = int.Parse(channel);
                messages = ChatManagement.GetChannelMessages(channelID);
                MessagesList.DataSource = messages;
                MessagesList.DataBind();
            }
            else
            {
                string reciever = RecieverBox.Text;
                if (reciever != string.Empty)
                {
                    int senderID = int.Parse(messageSender);
                    int recieverID = int.Parse(reciever);
                    messages = ChatManagement.GetPrivateMessages(senderID, recieverID);
                    MessagesList.DataSource = messages;
                    MessagesList.DataBind();
                }
            }
        }

        protected void UsersList_ItemEditing(object sender, ListViewEditEventArgs e)
        {
            UsersList.EditIndex = e.NewEditIndex;
            UsersList.DataSource = ChatManagement.GetAllUsers();
            this.DataBind();
        }

        protected void UsersList_ItemInserting(object sender, ListViewInsertEventArgs e)
        {
            User newUser = new User()
            {
                UserName = (UsersList.InsertItem.FindControl("NewUsernameTextBox") as TextBox).Text,
                Password = (UsersList.InsertItem.FindControl("NewPasswordTextBox") as TextBox).Text,
                FirstName = (UsersList.InsertItem.FindControl("NewFirstNameTextBox") as TextBox).Text,
                LastName = (UsersList.InsertItem.FindControl("NewLastNameTextBox") as TextBox).Text,
                RegistrationDate = DateTime.Now,
                Administrator = (UsersList.InsertItem.FindControl("NewAdminCheckBox") as CheckBox).Checked,
                LastActivity = DateTime.Now
            };
            ChatManagement.CreateUser(newUser);
            UsersList.DataSource = ChatManagement.GetAllUsers();
            this.DataBind();
        }

        protected void UsersList_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
            int id = int.Parse((UsersList.Items[e.ItemIndex].FindControl("UserIDLabel") as Label).Text);
            User editUser = new User()
            {
                UserName = (UsersList.EditItem.FindControl("UserNameEditLabel") as TextBox).Text,
                Password = (UsersList.EditItem.FindControl("UserPasswordEditLabel") as TextBox).Text,
                FirstName = (UsersList.EditItem.FindControl("UserFirstNameEditLabel") as TextBox).Text,
                LastName = (UsersList.EditItem.FindControl("UserLastNameEditLabel") as TextBox).Text,
                RegistrationDate = DateTime.Now,
                Administrator = (UsersList.EditItem.FindControl("UserAdminCheckBox") as CheckBox).Checked,
                LastActivity = DateTime.Now
            };

            ChatManagement.EditUser(id, editUser);
            UsersList.EditIndex = -1;
            UsersList.DataSource = ChatManagement.GetAllUsers();
            this.DataBind();
        }

        protected void UsersList_ItemDeleting(object sender, ListViewDeleteEventArgs e)
        {
            int id = int.Parse((UsersList.Items[e.ItemIndex].FindControl("UserIDLabel") as Label).Text);
            ChatManagement.RemoveUserMessages(id);
            ChatManagement.DeleteUser(id);
            UsersList.DataSource = ChatManagement.GetAllUsers();
            this.DataBind();
        }

        protected void ChannelsList_ItemInserting(object sender, ListViewInsertEventArgs e)
        {
            var a = ChannelsList.InsertItem.FindControl("NewChannelTextBox");
            ChatDAL.Channel newChannel = new ChatDAL.Channel()
            {
                ChannelName = (ChannelsList.InsertItem.FindControl("NewChannelTextBox") as TextBox).Text,
                Description = (ChannelsList.InsertItem.FindControl("NewDescriptionTextBox") as TextBox).Text
            };
            ChatManagement.CreateChannel(newChannel);
            ChannelsList.DataSource = ChatManagement.GetAllChannels();
            this.DataBind();
        }

        protected void ChannelsList_ItemDeleting(object sender, ListViewDeleteEventArgs e)
        {
            int id = int.Parse((ChannelsList.Items[e.ItemIndex].FindControl("IDLabel1") as Label).Text);
            ChatManagement.DeleteChannel(id);
            ChannelsList.DataSource = ChatManagement.GetAllChannels();
            this.DataBind();
        }

        protected void ChannelsList_ItemEditing(object sender, ListViewEditEventArgs e)
        {
            ChannelsList.EditIndex = e.NewEditIndex;
            ChannelsList.DataSource = ChatManagement.GetAllChannels();
            this.DataBind();
        }

        protected void ChannelsList_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
            int id = int.Parse((ChannelsList.Items[e.ItemIndex].FindControl("ChannelIDLabel") as Label).Text);
            ChatDAL.Channel newChannel = new ChatDAL.Channel()
            {
                ChannelName = (ChannelsList.EditItem.FindControl("ChannelNameEditLabel") as TextBox).Text,
                Description = (ChannelsList.EditItem.FindControl("DescriptionEditLabel") as TextBox).Text
            };

            ChatManagement.EditChannel(id, newChannel);
            ChannelsList.EditIndex = -1;
            ChannelsList.DataSource = ChatManagement.GetAllChannels();
            this.DataBind();
        }
    }
}